package com.eibus.applicationconnector.dotnet;

import junit.framework.TestCase;
import com.eibus.connector.nom.Connector;
import com.eibus.xml.nom.Document;
import com.eibus.xml.nom.Node;
import com.eibus.xml.xpath.XPath;

//@Author	: Lejil
//@Class	: DotnetReferenceArgumentsTest

/**
 *	<br/>Owner:			Lejil
 *	<br/>Description:	Invoke the .NET public methods with ByRef Arguments, For all the dataTypes with incorrect values 
 */
public class DotnetReferenceArgumentsTestCase extends TestCase
{

	Connector  connector;
	
	protected void setUp()
	{
		try
		{
			connector = Connector.getInstance("DotNET Client");
			if (!connector.isOpen())
				connector.open();
		}
		catch(Exception ex)
		{
			fail("Could not get instance of Connector. "+ex.getMessage());
		}
	}
	
	protected void tearDown()
	{
	}

	public DotnetReferenceArgumentsTestCase(String name)
	{
		super(name);
	}


	public static void main(String[] args)
	{
		junit.textui.TestRunner.run(DotnetReferenceArgumentsTestCase.class);
	}



	public int  sendRequest(String methodName,String argsOne,String argsTwo)
	{
		int request=0,response=0;
		try
		{
			request = connector.createSOAPMethod("http://schemas.cordys.com/1.0/dotnet/RefUnitTest",methodName);
			Node.createTextElement("args1",argsOne,request);
			Node.createTextElement("args2",argsTwo,request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method :- " + methodName +" Exception :- " + ex.toString());
		}
		finally
		{
			Util.delete(Node.getRoot(request));
		}
		return response;
	}

	public  boolean checkforSoapFault(int response) throws Exception
	{
		try
		{
			int  faultNode = XPathUtil.getSOAPFault(response);
			if ( faultNode != 0)
			{
				fail(".NET Connector junit Method Failed with SoapFault	:-"+Node.writeToString(faultNode,true));
			}
		}
		catch(Exception ex)
		{
			throw ex;
		}
		return true;
	}
    
    private void commonTestMethod(String methodName, String argsOne, String argsTwo, String searchXPath, String expectedResult)
    {
        int response =0;
        try
        {
            response = sendRequest(methodName, argsOne, argsTwo);            
            String result = Node.getDataWithDefault(
                    new XPath(searchXPath).firstMatch(response, null),
                    "Not Found");
            
            if (!result.equalsIgnoreCase(expectedResult))
            {
                //fail("testBooleanType Expected value is True, but returned False");
                System.out.println(methodName);
                System.out.println("Expected value is : " + expectedResult);
                System.out.println("Result value is : " + result);
            }
            //assertEquals(expectedResult, result);
        }
        catch(Exception ex)
        {
            System.out.println(ex);
            fail(".NET Connector junit Failed for the .NET Method :- " + methodName);
        }
        finally
        {
            Util.delete(Node.getRoot(response));
        }
    }

	public void testrefBooleanArgs() //1
	{
        commonTestMethod("TestBoolType","true","false", ".//TestBoolType/TestBoolType", "True");
	}


	public void testrefByteArgs() //2
	{
        commonTestMethod("TestByteType","0","1", ".//TestByteType/TestByteType", "0");
	}

	public void testrefCharArgs() //3
	{
        commonTestMethod("TestCharType","a","b", ".//TestCharType/TestCharType", "a");
	}
    
	public void testrefDoubleArgs() //4
	{
        commonTestMethod("TestDoubleType","1003.3464","20.23", ".//TestDoubleType/TestDoubleType", "1003.3464");
	}
    
	public void testrefIntArgs() //5
	{
        commonTestMethod("TestIntegerType","999999","2888", ".//TestIntegerType/TestIntegerType", "999999");
	}
    
	public void testrefLongArgs() //6
	{
        commonTestMethod("TestLongType","1234567891234567890","12344", ".//TestLongType/TestLongType", "1234567891234567890" );
	}
    
	public void testrefShortArgs() //7
	{
        commonTestMethod("TestShortType","12345","124", ".//TestShortType/TestShortType", "12345");
	}
    
	public void testrefSingleArgs() //8
	{
        commonTestMethod("TestSingleType","12.53","23.5", ".//TestSingleType/TestSingleType", "12.53");
	}
	

	
	public void testrefStringArgs() //9
	{
        commonTestMethod("TestStringType","cordys india pvt ltd","India", ".//TestStringType/TestStringType", "cordys india pvt ltd");
	}
	
	
	public void testrefXMLArgs() //10
	{
        commonTestMethod("TestXmlType","<a>Test</a>","<b>Test</b>", ".//TestXmlType/TestXmlType", "Test");
	}
	
	public void testrefDateTimeArgs() //11
	{
        commonTestMethod("TestDateTimeType","8/23/2004 12:00:00 AM","2004-08-23T00:00:00.00", 
                ".//TestDateTimeType/TestDateTimeType", "8/23/2004 12:00:00 AM");
	}
	
	public void testrefXMLNomArgs() //12
	{
		int response =0;
		Document document = null;

		try
		{
			document = new Document();
			int node = document.parseString("<location>Hyderabad</location>");
			response = sendRequest("TestXMLNomType",node+"",node+"");
			//String result = Find.firstDataWithDefault(response,"?<TestXMLNomType><TestXMLNomType>","10.20");
            String result = Node.getDataWithDefault(
                    new XPath(".//TestXMLNomType/TestXMLNomType").firstMatch(response, null),
                    "10.20");
			int dataNode		= Integer.parseInt(result);
			String data		= Node.getDataWithDefault(dataNode,"Falied");
			if (!data.equals("Hyderabad"))
			{
				fail("TestXMLNomType Expected value is -<location>Hyderabad</location>, but returned " + result);
			}
			Util.delete(node);
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method : TestXMLNomType");
		}
		finally
		{
			Util.delete(Node.getRoot(response));
			document = null;
		}
	}
	

	// STATIC METHODS
	//
	
	
	
	public void testStaticrefBooleanArgs() //1
	{
        commonTestMethod("TestStaticBoolType","true","false", ".//TestStaticBoolType/TestStaticBoolType", "True");
	}


	public void testStaticrefByteArgs() //2
	{ 
        commonTestMethod("TestStaticByteType","0","1", ".//TestStaticByteType/TestStaticByteType", "0");
	}

	public void testStaticrefCharArgs() //3
	{
        commonTestMethod("TestStaticCharType","a","b", ".//TestStaticCharType/TestStaticCharType", "a");
	}
    
	public void testStaticrefDoubleArgs() //4
	{
        commonTestMethod("TestStaticDoubleType","1003.3464","20.23", ".//TestStaticDoubleType/TestStaticDoubleType", "1003.3464");
	}
    
	public void testStaticrefIntArgs() //5
	{
        commonTestMethod("TestStaticIntegerType","999999","2888", ".//TestStaticIntegerType/TestStaticIntegerType", "999999");
	}
    
	public void testStaticrefLongArgs() //6
	{
        commonTestMethod("TestStaticLongType","1234567891234567890","12344", ".//TestStaticLongType/TestStaticLongType", "1234567891234567890");
	}
    
	public void testStaticrefShortArgs() //7
	{  
        commonTestMethod("TestStaticShortType","12345","124", ".//TestStaticShortType/TestStaticShortType", "12345");
	}
    
	public void testStaticrefSingleArgs() //8
	{
        commonTestMethod("TestStaticSingleType","12.53","23.5", ".//TestStaticSingleType/TestStaticSingleType", "12.53");
	}
	
	public void testStaticrefStringArgs() //9
	{
        commonTestMethod("TestStaticStringType","cordys india pvt ltd","India", ".//TestStaticStringType/TestStaticStringType", "cordys india pvt ltd");
	}
    
	public void testStaticrefXMLArgs() //10
	{
        commonTestMethod("TestStaticXmlType","<a>Test</a>","<b>Test</b>", ".//TestStaticXmlType/TestStaticXmlType" ,"Test");
	}
	
	public void testStaticrefDateTimeArgs() //11
	{
        commonTestMethod("TestStaticDateTimeType","8/23/2004 12:00:00 AM","2004-08-23T00:00:00.00",
                ".//TestStaticDateTimeType/TestStaticDateTimeType", "8/23/2004 12:00:00 AM");
	}
	
	public void testStaticrefXMLNomArgs() //12
	{
		int response =0;
		Document document = null;

		try
		{
			document = new Document();
			int node = document.parseString("<location>Hyderabad</location>");
			response = sendRequest("TestStaticXMLNomType",node+"",node+"");
			//String result	= Find.firstDataWithDefault(response,"?<TestStaticXMLNomType><TestStaticXMLNomType>","10.20");;
            String result = Node.getDataWithDefault(
                    new XPath(".//TestStaticXMLNomType/TestStaticXMLNomType").firstMatch(response, null),
                    "10.20");
			int dataNode	= Integer.parseInt(result);
			String data		= Node.getDataWithDefault(dataNode,"Falied");
			if (!data.equals("Hyderabad"))
			{
				fail("TestStaticXMLNomType Expected value is -<location>Hyderabad</location>, but returned " + result);
			}
			Util.delete(node);
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method : TestStaticXMLNomType");
		}
		finally
		{
			Util.delete(Node.getRoot(response));
			document = null;
		}
	}	
}
